import houseOpt from '@/biz/option/house.js'
import channelMgr from '@/biz/channel/channel'
import apiMgr from '@/biz/caseReceiver/index'
import buildingApi from '@/config/server/api/building'

let _this = null

function getHouse(housesName) {
  return buildingApi.house.names
    .fetch({
      housesName
    })
    .then((res) => {
      let houses = res.list.map((item) => {
        item.key = item.housesId
        item.value = item.housesName
        return item
      })
      return houses
    })
}

function houseInfo(info) {
  let opt = {
    col: 12,
    title: '案场',
    key: 'buildingId',
    load: (o) => {
      if (info.buildingId) {
        o.select.options = [
          {
            key: info.buildingId,
            value: info.housesName
          }
        ]
      }
    },
    select: {
      filterable: true,
      options: [],
      remote: (key) => {
        return getHouse(key)
      },
      data: {
        to: (info) => {
          return { housesId: info.houseId, housesName: info.housesName }
        }
      }
    },
    change: (data) => {
      _this.getInit(data.value)
    },
    rules: [
      {
        required: true,
        message: '请选择案场',
        trigger: 'blur'
      }
    ]
  }

  return opt
}

function channelConfig() {
  let switchRow = [
    {
      title: '同策好房',
      key: 'hfFlag',
      col: 12,
      switch: {
        openText: '开',
        closeText: '关',
        before: () => {
          if (_this.info.hfFlag) {
            return new Promise((resolve) => {
              _this.$Modal.confirm({
                title: '关闭投放渠道确认',
                content: `关闭投放渠道后会释放该渠道下案场和个人的呼入号，是否确定要关闭`,
                okText: '是',
                cancelText: '否',
                closable: true,
                onOk: () => {
                  apiMgr.clearAllUndertakings({ buildingId: _this.info.buildingId, type: 1 }).then(() => {
                    _this.$Message.success('关闭成功')
                    _this.$emit('success')
                    selectChannel[1].select.options = []
                    return resolve()
                  })
                }
              })
            })
          } else {
            return Promise.resolve()
          }
        }
      },
      change: (data) => {
        if (!data.value) {
          selectChannel[0].visible = true
          selectChannel[1].visible = false
          buildingIds[0].visible = true
          buildingIds[1].visible = false
          _this.info.hfChannel = ''
          _this.info.hfIds = ''
        } else {
          selectChannel[0].visible = false
          selectChannel[1].visible = true
          buildingIds[0].visible = false
          buildingIds[1].visible = true
        }
      }
    },
    {
      title: '百度地图',
      key: 'bdFlag',
      col: 12,
      switch: {
        openText: '开',
        closeText: '关',
        before: () => {
          if (_this.info.bdFlag) {
            return new Promise((resolve) => {
              _this.$Modal.confirm({
                title: '关闭投放渠道确认',
                content: `关闭投放渠道后会释放该渠道下案场和个人的呼入号，是否确定要关闭`,
                okText: '是',
                cancelText: '否',
                closable: true,
                onOk: () => {
                  apiMgr.clearAllUndertakings({ buildingId: _this.info.buildingId, type: 2 }).then(() => {
                    _this.$Message.success('关闭成功')
                    _this.$emit('success')
                    selectChannel[2].select.options = []
                    return resolve()
                  })
                }
              })
            })
          } else {
            return Promise.resolve()
          }
        }
      },
      change: (data) => {
        if (!data.value) {
          selectChannel[2].visible = false
          buildingIds[2].visible = false
          _this.info.bdChannel = ''
          _this.info.bdIds = ''
        } else {
          selectChannel[2].visible = true
          buildingIds[2].visible = true
        }
      }
    }
  ]
  let selectChannel = [
    {
      col: 12,
      visible: !_this.info.hfFlag
    },
    {
      col: 12,
      title: '线索来源渠道',
      key: 'hfChannel',
      visible: _this.info.hfFlag,
      load: (o) => {
        channelMgr.getClueChannelList().then((res) => {
          res.map((item) => {
            //10008:同策好房  10012:百度地图
            let obj = {}
            if (item.id == '10008' || item.id == '10012') {
              obj.key = item.id
              obj.value = item.channelName
              o.select.options.push(obj)
            }
          })
        })
      },
      select: {
        options: []
      },
      rules: [
        {
          required: true,
          message: '请选择线索来源渠道',
          trigger: 'blur'
        }
      ]
    },
    {
      col: 12,
      title: '线索来源渠道',
      key: 'bdChannel',
      visible: _this.info.bdFlag,
      load: (o) => {
        channelMgr.getClueChannelList().then((res) => {
          res.map((item) => {
            //10008:同策好房  10012:百度地图
            let obj = {}
            if (item.id == '10008' || item.id == '10012') {
              obj.key = item.id
              obj.value = item.channelName
              o.select.options.push(obj)
            }
          })
        })
      },
      select: {
        options: []
      },
      rules: [
        {
          required: true,
          message: '请选择线索来源渠道',
          trigger: 'blur'
        }
      ]
    }
  ]
  let buildingIds = [
    {
      col: 12,
      visible: !_this.info.hfFlag
    },
    {
      col: 12,
      title: '同策好房楼盘Id',
      key: 'hfIds',
      placeholder: '请输入同策好房楼盘Id',
      visible: _this.info.hfFlag,
      input: {},
      tip: '（多楼盘id用逗号隔开）',
      tipsColor: '#ccc',
      rules: [
        {
          required: true,
          message: '请输入同策好房楼盘Id',
          trigger: 'blur'
        }
      ]
    },
    {
      col: 12,
      title: '百度地图楼盘Id',
      key: 'bdIds',
      placeholder: '请输入百度地图楼盘Id',
      visible: _this.info.bdFlag,
      input: {},
      tip: '（多楼盘id用逗号隔开）',
      tipsColor: '#ccc',
      rules: [
        {
          required: true,
          message: '请输入百度地图楼盘Id',
          trigger: 'blur'
        }
      ]
    }
  ]
  return [switchRow, selectChannel, buildingIds]
}
function options(vm) {
  _this = vm
  let arr = [
    // houseInfo(_this.info),
    houseInfo(_this.info),
    {
      title: '',
      key: 'channelLabel',
      text: {
        fontSize: '15px',
        color: '#F59A23',
        fontWeight: 'bold'
      }
    },
    ...channelConfig()
  ]

  return arr
}

export default {
  options
}
